package com.fzk.blog.settings.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.NumberUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fzk.blog.bean.DicType;
import com.fzk.blog.bean.DicValue;
import com.fzk.blog.bean.vo.DicInfo;
import com.fzk.blog.exception.MyException;
import com.fzk.blog.settings.service.IDicService;
import com.fzk.blog.utils.ResponseResult;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author fzk
 * @date 2021-08-19 22:36
 */
@RestController
public class DicController {
    @Resource
    private IDicService dicService;

    @GetMapping("/dic/info")
    public Map<String, Object> getDicInfoListOrOne(
            @RequestParam(name = "dic_type_id", required = false) String dicTypeId,
            @RequestParam(name = "dic_type_name", required = false) String dicTypeName
    ) throws JsonProcessingException {
        // 1.判断参数
        if (dicTypeId == null) {
            List<DicInfo> dicInfos = null;
            // 1.1查询所有列表
            if (dicTypeName == null)
                dicInfos = dicService.getDicInfoList();
            else if (Strings.trimToNull(dicTypeName) == null)
                throw new MyException(400, "参数错误：dic_type_name：" + dicTypeName);
                // 1.2 查询相应类型名称的dicInfoList
            else
                dicInfos = dicService.getDicInfoByTypeName(dicTypeName);
            return ResponseResult.success("查询成功", dicInfos);
        } else if (!NumberUtil.isNumber(dicTypeId))
            throw new MyException(400, "参数错误：dic_type_id：" + dicTypeId);
        else {
            // 查询特定dicInfo
            List<DicInfo> dicInfoList = dicService.getDicInfoByTypeId(Integer.parseInt(dicTypeId));
            return ResponseResult.success("查询成功", dicInfoList);
        }
    }

    @GetMapping("/dic/type")
    public Map<String, Object> getDicTypeListOrOne(
            @RequestParam(name = "id", required = false) String id
    ) throws JsonProcessingException {
        // 1.判断参数
        if (id == null) {
            // 查询列表
            List<DicType> dicTypeList = dicService.getDicTypeList();
            return ResponseResult.success("查询成功", dicTypeList);
        } else if (!NumberUtil.isNumber(id))
            throw new MyException(400, "参数错误：id：" + id);
        else {
            // 查询特定dicType
            DicType dicType = dicService.getDicType(Integer.parseInt(id));
            return ResponseResult.success("查询成功", dicType);
        }
    }

    @PostMapping("/dic/type")
    @SaCheckRole("root")//检查角色
    public Map<String, Object> insertDicType(
            @RequestParam("name") String name,
            @RequestParam("description") String description
    ) {
        // 1.参数检查
        checkArgs(name, description);

        // 2.取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());

        // 3.装入参数
        DicType dicType = new DicType();
        dicType.setName(name);
        dicType.setDescription(description);

        // 4.调用service
        List<DicInfo> dicInfoList = dicService.insertDicType(dicType, loginId);

        return ResponseResult.success("添加成功", dicInfoList);
    }

    @PutMapping("/dic/type")
    @SaCheckRole("root")//检查角色
    public Map<String, Object> updateDicType(
            @RequestParam("id") int id,
            @RequestParam("name") String name,
            @RequestParam("description") String description
    ) {
        // 1.参数检查
        checkArgs(name, description);

        // 2.取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());

        // 3.装入参数
        DicType dicType = new DicType();
        dicType.setId(id);
        dicType.setName(name);
        dicType.setDescription(description);

        // 4.调用service
        List<DicInfo> dicInfoList = dicService.updateDicType(dicType, loginId);

        return ResponseResult.success("修改成功", dicInfoList);
    }

    @DeleteMapping("/dic/type")
    @SaCheckRole("root")//检查角色
    public Map<String, Object> deleteDicType(
            @RequestParam("id") int id
    ) {
        // 1.取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());

        // 2.调用service
        List<DicInfo> dicInfoList = dicService.deleteDicType(id, loginId);

        return ResponseResult.success("删除成功", dicInfoList);
    }

    @PostMapping("/dic/value")
    @SaCheckRole("root")//检查角色
    public Map<String, Object> insertDicValue(
            @RequestParam("dic_type_id") int dicTypeId,
            @RequestParam("name") String name,
            @RequestParam("value") String value
    ) {
        // 1.参数检查
        checkArgs(name, value);

        // 2.取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());

        // 3.装入参数
        DicValue dicValue = new DicValue();
        dicValue.setDicTypeId(dicTypeId);
        dicValue.setName(name);
        dicValue.setValue(value);

        // 4.调用service
        List<DicInfo> dicInfoList = dicService.insertDicValue(dicValue, loginId);

        return ResponseResult.success("添加成功", dicInfoList);
    }

    @PutMapping("/dic/value")
    @SaCheckRole("root")//检查角色
    public Map<String, Object> updateDicValue(
            @RequestParam("id") int id,
            @RequestParam("name") String name,
            @RequestParam("value") String value
    ) {
        // 1.参数检查
        checkArgs(name, value);

        // 2.取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());

        // 3.装入参数
        DicValue dicValue = new DicValue();
        dicValue.setId(id);
        dicValue.setName(name);
        dicValue.setValue(value);

        // 4.调用service
        dicService.updateDicValue(dicValue, loginId);

        return ResponseResult.success("修改成功", null);
    }

    @DeleteMapping("/dic/value")
    @SaCheckRole("root")//检查角色
    public Map<String, Object> updateDicValue(
            @RequestParam("id") int id
    ) {
        // 1.取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());

        // 2.调用service
        dicService.deleteDicValue(id, loginId);

        return ResponseResult.success("删除成功", null);
    }


    /**
     * 参数检查
     *
     * @param args 可变形参
     */
    private void checkArgs(String... args) {
        for (String arg : args) {
            if (arg == null || arg.length() == 0)
                throw new MyException(400, "参数错误：" + arg);
        }
    }
}
